<template>
  <div>
    <div v-click-outside="handleClose" class="dropdown">
      <button @click="isOpen = !isOpen">打开/ 关闭</button>
      <div v-show="isOpen" class="menu">这是一个下拉菜单</div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
const isOpen = ref(false);
function handleClose() {
  isOpen.value = false;
}
const vClickOutside = {
  mounted(el, binding) {
    const handleOutsideClick = (e) => {
      console.log("binding", binding, el, e.target);

      if (!el.contains(e.target)) {
        binding.value();
      }
    };
    document.addEventListener("click", handleOutsideClick);
    el._onDestory = () => {
      document.removeEventListener("click", handleOutsideClick);
    };
  },
  unmounted(el) {
    el._onDestory();
  },
};
</script>

<style></style>
